import { formatGMTDate, getYMD, getNowFormatDate, getDay } from '../../../../assets/js/util.js'
export default {
    data() {
        return {
            // 默认参数 和 URL
            SRC: '',
            DP: {
                p: '',
                f: '',
                uid: ''
            },
            /*=========时间段配置=========*/
            pickerOptions: {
                shortcuts: [{
                    text: '近7天',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '近15天',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 15);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '近30天',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                        picker.$emit('pick', [start, end]);
                    }
                }]
            },
            title: 'vip统计',
            agents: [],
            platForms: [],
            settlWays: [],
            channels: [],
            ruleForm: {
                date: '',
                tjDate: '',
                platForm: '',
                agent: '',
                settlWay: '',
                channel: '',
            },
            /*=================表1数据=================*/
            tableHead: [{
                    "prop": "logday",
                    "label": "日期"
                },
                {
                    "prop": "channelName",
                    "label": "渠道名称"
                },
                {
                    "prop": "agent",
                    "label": "代理公司"
                },
                {
                    "prop": "active",
                    "label": "广告激活设备"
                },
                {
                    "prop": "warnActice",
                    "label": "异常激活设备"
                },
                {
                    "prop": "rate",
                    "label": "异常激活率"
                }
            ],
            activeIndex: '0',
            loading: false,
            searchable: false,
            tableData: [],
            tableDataAll: [],
            /*当前页显示数量*/
            pageSize: 10,
            /*每页显示的数量*/
            pageSizes: [10, 20, 50, 100],
            /*dataTotal表数量*/
            dataTotal: 0,
            /*分页器*/
            currentPage: 1,
        }
    },
    computed: {

    },
    methods: {
        // 通用 成功 与 失败
        successFun(data) {
            this.$message({
                message: data,
                type: 'success'
            });
        },
        errorFun(data) {
            this.$message.error(data);
        },
        dateChange(val) {
            // this.ruleForm.date = val;
        },
        agentChange() {

        },
        platFormChange() {

        },
        channelChange() {

        },
        settlWayChange() {

        },
        handleSizeChange(val) {
            this.pageSize = val;

        },
        handleCurrentChange(val) {
            this.currentPage = val;

        },
        tableControl() {
            this.tableData = this.tableDataAll.slice((this.currentPage - 1) * this.pageSize, this.currentPage * this.pageSize)
        },
        loads() {
            this.loading = !this.loading; //加载中
            this.searchable = !this.searchable; //查询按钮不可用
        },
        /*==================获取下拉列表数据========================*/
        getData() {
            this.$http.jsonp(`${this.SRC}data/get.do?p=${this.DP.p}&f=${this.DP.f}&a=initParam`, {
                params: { 'ps': `uid=${this.DP.uid}` }
            }).then(function(res) {
                var data = JSON.parse(JSON.parse(res.body));
                if (parseInt(data['res']) === 0) {
                    /*=====获取平台======*/
                    for (var i = 0; i < data.platform.length; i++) {
                        var obj = {};
                        obj.id = data.platform[i];
                        obj.platFormName = data.platform[i];
                        this.platForms.push(obj);
                    }
                    /*=====获取代理商数据======*/
                    for (var i = 0; i < data.agent.length; i++) {
                        var obj = {};
                        obj.id = data.agent[i];
                        obj.agentName = data.agent[i];
                        this.agents.push(obj);
                    }
                    /*=====获取结算方式数据======*/
                    for (var i = 0; i < data.paytype.length; i++) {
                        var obj = {};
                        obj.id = data.paytype[i];
                        obj.settlWayName = data.paytype[i];
                        this.settlWays.push(obj);
                    }
                    /*=====获取渠道名称======*/
                    for (var i = 0; i < data.channelName.length; i++) {
                        var obj = {};
                        obj.id = data.channelName[i];
                        obj.channelName = data.channelName[i];
                        this.channels.push(obj);
                    }
                } else {
                    this.errorFun(data['msg'])
                }
            })
        },
        /*====================时间格式化转换函数============================*/
        formatDate(date) {
            var y = date.getFullYear();
            var m = date.getMonth() + 1;
            m = m < 10 ? '0' + m : m;
            var d = date.getDate();
            d = d < 10 ? ('0' + d) : d;
            return y + '-' + m + '-' + d;
        },
        /*==================获取表格数据========================*/
        getTableData(data, btn) {
            this.activeIndex = btn;
            this.loads();
            let date1 = this.formatDate(this.ruleForm.date[0]);
            let date2 = this.formatDate(this.ruleForm.date[1]);
            let xdate = this.formatDate(this.ruleForm.tjDate);
            this.$http.jsonp(`${this.SRC}data/get.do?p=${this.DP.p}&f=${this.DP.f}&a=search`, {
                params: { 'ps': `sday=${date1}&eday=${date2}&day=${xdate}&agent=${data.agent}&platform=${data.platForm}&channelName=${data.channel}&paytype=${data.settlWay}&uid=${this.DP.uid}` }
            }).then(function(res) {
                var data = JSON.parse(JSON.parse(res.body));
                this.loads();
                if (parseInt(data['res']) === 0) {
                    this.tableHead = data.columns;
                    this.tableData = [];
                    this.tableDataAll = [];
                    if (Number(btn) == 0) {
                        this.tableDataAll.push(data.accounts);
                        this.dataTotal = this.tableDataAll.length;
                        this.tableControl();
                    } else {
                        this.tableDataAll.push(data.accountRate);
                        this.dataTotal = this.tableDataAll.length;
                        this.tableControl();
                    }
                } else {
                    this.errorFun(data['msg'])
                }
            })
        },
        /*===========================查询功能===============================*/
        search(data, btn) {
            this.activeIndex = this.activeIndex;
            this.loads();
            let date1 = this.formatDate(this.ruleForm.date[0]);
            let date2 = this.formatDate(this.ruleForm.date[1]);
            let xdate = this.formatDate(this.ruleForm.tjDate);
            this.$http.jsonp(`${this.SRC}data/get.do?p=${this.DP.p}&f=${this.DP.f}&a=search`, {
                params: { 'ps': `sday=${date1}&eday=${date2}&day=${xdate}&agent=${data.agent}&platform=${data.platForm}&channelName=${data.channel}&paytype=${data.settlWay}&uid=${this.DP.uid}` }
            }).then(function(res) {
                var data = JSON.parse(JSON.parse(res.body));
                this.loads();
                if (parseInt(data['res']) === 0) {
                    this.tableHead = data.columns;
                    this.tableDataAll = [];
                    if (Number(btn) == 0) {
                        this.tableDataAll.push(data.accounts);
                        this.dataTotal = this.tableDataAll.length;
                        this.tableControl();
                    } else {
                        this.tableDataAll.push(data.accountRate);
                        this.dataTotal = this.tableDataAll.length;
                        this.tableControl();
                    }
                } else {
                    this.errorFun(data['msg'])
                }
            })
        },
    },
    mounted() {
        // 初始化URL
        this.SRC = this.$store.getters.src;
        this.DP.p = localStorage.getItem('ZULONGBIPROJECTID');
        this.DP.f = localStorage.getItem('ZULONGBIPAGEID');
        this.DP.uid = localStorage.getItem('ZULONGBIUSERID');
        this.getData();
        this.ruleForm.date = [new Date(new Date().getTime() - 7 * 86400000), new Date()];
        this.ruleForm.tjDate = new Date(new Date().getTime() - 1 * 86400000);
        this.getTableData(this.ruleForm, '0');
    }
}